/*************************************************************************
> File Name: 01_test.cpp
> Author: hzy
> Created Time: 2023-08-15 19:37:29
> Description: 使用函数模板实现对 char 和 int 类型数组进行排序
************************************************************************/
#include <iostream>

using namespace std;

//类型模板参数
template <class T>
void PrintArray(T *arr, int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

//len是一个非类型模板参数，用于传递一个整型数值（常量）
template <typename T, int len>
void SortArr(T *arr)
{ 
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < len - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                T temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

template <class T>
void Swap()
{

}

void test_01() 
{
    int arr1[] = {21, 5, 8, 9, 46, 32, 45, 74, 71, 22};
    char arr2[] = "zxcvbnmas";

    //用const修饰const将len1设定为常变量
    const int len1 = sizeof(arr1) / sizeof(arr1[0]);
    const int len2 = sizeof(arr2) / sizeof(arr2[0]);

    PrintArray(arr1, len1);
    PrintArray(arr2, len2);
    cout << "===========" << endl;

    //len1必须为常量，否则编译会报错
    SortArr<int, len1>(arr1);
    SortArr<char, len2>(arr2);

    PrintArray(arr1, len1);
    PrintArray(arr2, len2);
}

int main()
{
    test_01();

    return 0;
}